package com.jmb.waimao.controller;

import com.icegreen.greenmail.util.GreenMail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Controller for testing GreenMail directly
 */
@RestController
@RequestMapping("/api/greenmail-test")
@Slf4j
public class GreenMailTestController {

    @Autowired
    private GreenMail greenMail;

    @Value("${greenmail.smtp.host:localhost}")
    private String smtpHost;

    @Value("${greenmail.smtp.port:3025}")
    private int smtpPort;

    @Value("${greenmail.user:user}")
    private String user;

    @Value("${greenmail.password:password}")
    private String password;

    @Value("${greenmail.email:user@localhost}")
    private String email;

    /**
     * Test sending an email directly through GreenMail
     *
     * @return Result of the operation
     */
    @GetMapping("/send-direct")
    public ResponseEntity<Map<String, Object>> sendDirectEmail() {
        Map<String, Object> result = new HashMap<>();

        try {
            // Set up mail session
            Properties props = new Properties();
            props.put("mail.smtp.host", smtpHost);
            props.put("mail.smtp.port", smtpPort);
            props.put("mail.smtp.auth", "true");
            Session session = Session.getInstance(props);

            // Create a message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(email));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com"));
            message.setSubject("Direct GreenMail Test");
            message.setText("This is a test email sent directly through GreenMail");

            // Send the message
            Transport transport = session.getTransport("smtp");
            transport.connect(smtpHost, smtpPort, user, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

            result.put("success", true);
            result.put("message", "Email sent successfully");
            
            // Check if the email was received by GreenMail
            MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
            result.put("receivedCount", receivedMessages.length);
            
        } catch (Exception e) {
            log.error("Error sending direct email", e);
            result.put("success", false);
            result.put("message", "Failed to send email: " + e.getMessage());
        }

        return ResponseEntity.ok(result);
    }

    /**
     * Get server status
     *
     * @return Server status
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getServerStatus() {
        Map<String, Object> result = new HashMap<>();
        
        result.put("running", greenMail.isRunning());
        result.put("smtpHost", smtpHost);
        result.put("smtpPort", smtpPort);
        result.put("user", user);
        result.put("email", email);
        
        return ResponseEntity.ok(result);
    }
}
